<?php

namespace App\Repositories;

use App\Models\TestingDevUrl;
use Cache;
class TestingDevUrlRepository extends Repository
{
    static $tag = 'testing.developer.url';


    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.developer_url.info') . $id;
        $value = $this->remember($cacheKey, function () use ($id) {
            return TestingDevUrl::where('id', $id)->first();
        });
        return $value;
    }

    
    public function getByUrlCode($redirectUrl,$unionCode){
//        $cacheKey = getCacheKey('testing.developer_url.dev_code') . $unionCode;
        $devUrlObj = TestingDevUrl::where('redirect_url',$redirectUrl)
                                    ->where('union_code',$unionCode)
                                    ->first();
        return $devUrlObj;
    }


    /**
     * 根据redirectUrl和unionCode获取对应的id
     */
    public function fetchRedirectUrlInfoId($redirectUrl,$unionCode){
        $info = [];
        if(!$info = $this->getByUrlCode($redirectUrl,$unionCode)){
            $info['redirect_url'] = $redirectUrl;
            $info['union_code'] = $unionCode;
            $result = $this->store($info);
            $id = $result['id'];
            $info['id'] = $id;
        }
        return $info['id'];
    }

     public function store($data)
     {
        $res = TestingDevUrl::firstOrCreate($data);
        return $res;
     }

    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(TestingDevUrl::class);
    }

    public function tag()
    {
        return self::$tag;
    }
}